package org.mercworks.knight.textures;

import java.awt.Image;
import java.net.URL;
import java.nio.ByteBuffer;

import org.mercworks.knight.util.ImageUtils;

public class KnightImage
{
	public static final int SIZE_BYTE = 1;
	public int h = 0;
	public int w = 0;
	public ByteBuffer pixelBuffer = null; // to store bytes in GL_RGBA format
	public int[] pixels = null;
	public Image image = null;

	public KnightImage(URL imageUrl)
	{
		Image tmpi;
		try
		{
			tmpi = ImageUtils.loadImageFromURL(imageUrl);
			
			if (tmpi != null)
			{
				w = tmpi.getWidth(null);
				h = tmpi.getHeight(null);
				image = tmpi;
				pixels = ImageUtils.getImagePixels(pixels, image, w, h); // pixels in default Java ARGB format
				pixelBuffer = ImageUtils.convertImagePixels(pixels, w, h, true); // convert to RGBA
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
  /**
   * return ByteBuffer containing pixels in RGBA format (commmonly used in OpenGL).
   */
  public ByteBuffer getPixelsRGBA()
  {
      return this.pixelBuffer;
  }
}
